package com.cms.gdm.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.cms.common.utils.R;
import com.cms.gdm.dao.GuideDao;
import com.cms.gdm.entity.GuideEntity;
import com.cms.gdm.entity.vo.GuideVo;
import com.cms.gdm.feign.StuFeign;
import com.cms.gdm.to.StudentAllTo;
import com.cms.gdm.to.StudentTo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cms.common.utils.PageUtils;
import com.cms.common.utils.Query;

import com.cms.gdm.dao.TeacherDao;
import com.cms.gdm.entity.TeacherEntity;
import com.cms.gdm.service.TeacherService;

import javax.annotation.Resource;


@Service("teacherService")
public class TeacherServiceImpl extends ServiceImpl<TeacherDao, TeacherEntity> implements TeacherService {

    @Resource
    private GuideDao guideDao;

    @Autowired
    private StuFeign stuFeign;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<TeacherEntity> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            wrapper.eq("id", key).or().like("name", key).or().like("subject", key);
        }
        IPage<TeacherEntity> page = this.page(
                new Query<TeacherEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public GuideVo selectStudentByTeacherId(Long teacherId) {
        GuideVo guideVo = new GuideVo();
        GuideEntity guideEntity = guideDao.selectOne(new QueryWrapper<GuideEntity>().eq("teacher_id", teacherId));
        BeanUtils.copyProperties(guideEntity, guideVo);
        TeacherEntity teacherEntity = baseMapper.selectOne(new QueryWrapper<TeacherEntity>().eq("id", teacherId));
        guideVo.setTeacherName(teacherEntity.getName());
        guideVo.setProjectName(teacherEntity.getSubject());
        R r = stuFeign.selectInfoById(guideEntity.getStudentId());
        if(r.getCode() == 0){
            StudentAllTo student = r.getData("student", new TypeReference<StudentAllTo>() {
            });
            guideVo.setStudentName(student.getName());
            guideVo.setStudent(student);
        }

        return guideVo;
    }

}